<template>
	<view class="home" @tap="handlePageClick">
		<custom-navbar title="意向空间" :show-back="true" @back="handleBack" />

		<view class="content" :style="{ paddingTop: navBarHeight + 'px' }">
			<!-- 空状态 -->
			<view v-if="isEmpty" class="empty-state fcolcc">
				<image class="empty-image" src="/static/images/common/empty.png"></image>
				<view class="empty-text">暂无意向空间数据</view>
				<up-button type="primary" shape="circle" @click="loadSpaceList(true)"
					style="width: 200rpx;margin-top: 30rpx;">
					重新加载
				</up-button>
			</view>

			<!-- 空间列表 -->
			<view v-else class="space-list p30">
				<view class="item mb20" v-for="(item,index) in spaceList" :key="index" @click="handleDetails(item)">
					<view class="bg">
						<image class="plantbg" :src='item.space_image' mode="aspectFill"></image>
						<view class="top fcc">
							<image class="rentout" :src="cdn('仅出租.svg')"></image>
							<view class="text f22bc99A2E">
								{{item.rent_nature_text}}
							</view>
						</view>
					</view>
					<view class="btm p30">
						<view class="f36bc0 mb18">{{item.space_title}}</view>
						<view class="flex mb30">
							<view class="f245c9 fc">
								<view>租价:</view>
								<view class="FF0C10">{{item.rent_price}}元/㎡·月</view>
							</view>
							<view class="f245c9 fc ml30">
								<view>建筑面积：</view>
								<view class="c398DFB">{{item.building_area}}㎡</view>
							</view>
						</view>
						<up-line></up-line>
						<view class="fcsb mt18">
							<view class="f245c9">地址：{{item.address}}</view>
							<view class="btn f20bcf fcc" @click.stop="handleReserve(item)">
								预约考察
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 加载更多 -->
			<view v-if="isLoadingMore" class="loading-more fcc">
				<up-loading-icon text='加载中...'></up-loading-icon>
			</view>
			<view v-if="noMoreData" class="no-more fcc">
				没有更多数据了
			</view>
		</view>
	</view>
</template>

<script setup>
	import CustomNavbar from '@/components/custom-navbar.vue'
	import {
		ref,
		onMounted,
		computed,
		onUnmounted,
		getCurrentInstance
	} from 'vue';
	import {
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		mySpace
	} from '@/api/user'
	// 获取全局cdn方法
	const {
		proxy
	} = getCurrentInstance()
	const cdn = (path) => proxy.$cdn + path

	// 系统信息
	const systemInfo = uni.getSystemInfoSync()
	const navBarHeight = computed(() => systemInfo.statusBarHeight + 44)

	// 数据状态
	const isLoading = ref(false)
	const isLoadingMore = ref(false)
	const noMoreData = ref(false)
	const isEmpty = ref(false)

	// 分页参数
	const page = ref(1)
	const limit = ref(10)
	const total = ref(0)

	// 空间列表数据
	const spaceList = ref([])

	// 初始化加载
	onMounted(() => {
		loadSpaceList(true)
	})

	// 加载空间列表
	const loadSpaceList = async (isRefresh = false) => {
		if (isLoading.value && !isRefresh) return

		try {
			if (isRefresh) {
				page.value = 1
				noMoreData.value = false
				isLoading.value = true
			} else {
				isLoadingMore.value = true
			}

			const params = {
				page: page.value,
				limit: limit.value
			}

			const res = await mySpace(params)

			if (res.code === 200) {
				if (isRefresh) {
					spaceList.value = res.data.data
					total.value = res.data.total
				} else {
					spaceList.value = [...spaceList.value, ...res.data.data]
				}

				// 检查是否还有更多数据
				if (res.data.data.length < limit.value) {
					noMoreData.value = true
				}

				// 检查空状态
				isEmpty.value = spaceList.value.length === 0
			}
		} catch (error) {
			console.error('加载空间列表失败:', error)
			uni.showToast({
				title: '加载失败，请重试',
				icon: 'none'
			})
		} finally {
			isLoading.value = false
			isLoadingMore.value = false
		}
	}

	// 处理预约操作
	const handleReserve = (item) => {
		uni.navigateTo({
			url: `/pages/plant/reserve?space_id=${item.space_id}`
		})
	}

	// 查看详情
	const handleDetails = (item) => {
		uni.navigateTo({
			url: `/pages/plant/details?space_id=${item.space_id}`
		})
	}

	// 返回
	const handleBack = () => {
		uni.switchTab({
			url: '/pages/user/user'
		})
	}

	// 点击页面其他区域
	const handlePageClick = () => {
		// 可以用于关闭弹窗等操作
	}

	// 加载更多
	onReachBottom(() => {
		if (isLoadingMore.value || noMoreData.value) return
		page.value += 1
		loadSpaceList()
	})
</script>

<style lang="scss" scoped>
	.home {
		width: 100%;
		min-height: 100vh;
		background-color: #f7f7f7;
		position: relative;

		.content {
			padding-top: var(--status-bar-height);
			padding-top: calc(var(--status-bar-height) + 70rpx);

			.empty-state {
				height: 60vh;
				padding: 40rpx;
				text-align: center;

				.empty-image {
					width: 300rpx;
					height: 300rpx;
					margin-bottom: 30rpx;
				}

				.empty-text {
					font-size: 28rpx;
					color: #999;
				}
			}

			.space-list {
				.item {
					background: #FFFFFF;
					border-radius: 20rpx;
					overflow: hidden;
					box-shadow: 0rpx 4rpx 15rpx 0rpx rgba(0, 0, 0, 0.05);

					.bg {
						width: 100%;
						height: 320rpx;
						position: relative;

						.plantbg {
							width: 100%;
							height: 100%;
						}

						.top {
							position: absolute;
							right: 30rpx;
							top: 0;
							flex-direction: column;

							.rentout {
								width: 100rpx;
								height: 94rpx;
							}

							.text {
								position: absolute;
								top: 44rpx;
								color: #99A2E5;
								font-size: 22rpx;
								font-weight: bold;
							}
						}
					}

					.btm {
						.f36bc0 {
							font-size: 36rpx;
							font-weight: bold;
							color: #333;
						}

						.f245c9 {
							font-size: 24rpx;
							color: #5c5c5c;
						}

						.FF0C10 {
							color: #FF0C10;
						}

						.c398DFB {
							color: #398DFB;
						}

						.btn {
							width: 150rpx;
							height: 50rpx;
							background: linear-gradient(90deg, #398DFB 0%, #61DBFF 100%);
							border-radius: 10rpx;
							color: #FFFFFF;
						}
					}
				}
			}

			.loading-more,
			.no-more {
				padding: 30rpx 0;
				color: #999;
				font-size: 26rpx;
			}
		}
	}

	/* 通用样式 */
	.fcolcc {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.fcsb {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.fc {
		display: flex;
		align-items: center;
	}

	.fcc {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.p30 {
		padding: 30rpx;
	}

	.mb18 {
		margin-bottom: 18rpx;
	}

	.mb20 {
		margin-bottom: 20rpx;
	}

	.mb30 {
		margin-bottom: 30rpx;
	}

	.mt18 {
		margin-top: 18rpx;
	}

	.ml30 {
		margin-left: 30rpx;
	}
</style>